package com.hrserviceserve.controller;

import cn.hutool.core.lang.id.NanoId;

import com.hrserviceserve.dao.TransferRepository;
import com.hrserviceserve.entity.ListQuery;
import com.hrserviceserve.entity.Personnel;
import com.hrserviceserve.entity.Result;
import com.hrserviceserve.entity.Transfer;
import com.hrserviceserve.service.PersonnelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Api(value = "员工接口")
@RequestMapping("/personnel")
public class PersonnelController {
    @Autowired
    PersonnelService personnelService;
    @Autowired
    TransferRepository transferRepository;
    @ApiOperation(value = "获取全部员工分页")
    @GetMapping
    public Result findAll(@RequestParam("page") Integer page,@RequestParam("limit") Integer limit,
                          @RequestParam(value = "name",required = false) String name,
                          @RequestParam(value = "department",required = false) String department,
                          @RequestParam(value = "sex",required = false) String sex ){
        ListQuery listQuery=new ListQuery(page,limit,department,sex,name);
        Page byPage = personnelService.findByPage(listQuery);
        Map<String,Object> map=new HashMap<>();
        map.put("total",byPage.getTotalElements());
        map.put("data",byPage.getContent());
        return Result.success(map);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        Personnel byId = personnelService.findById(id);
        List<Personnel> personnels=new ArrayList<>();
        personnels.add(byId);
        return Result.success(personnels);
    }
    @PostMapping
    public Result addPersonnel(@RequestBody Personnel personnel){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        if (personnel.getPersonnelId()!=0){
            Personnel old=personnelService.findById(personnel.getPersonnelId());
//            if (old.getDepartment()!=personnel.getDepartment()||old.getPosition()!=personnel.getPosition()){
//                Transfer transfer=new Transfer( old.getPersonnelId(), old.getDepartment(), old.getPosition(),
//                        personnel.getDepartment(),personnel.getPosition(),df.format(System.currentTimeMillis()) );
//                transferRepository.save(transfer);
//            }
            if (old.getDepartment().equals(personnel.getDepartment()) && old.getPosition().equals(personnel.getPosition())){

            }else {
                Transfer transfer=new Transfer( old.getPersonnelId(), old.getDepartment(), old.getPosition(),
                        personnel.getDepartment(),personnel.getPosition(),df.format(System.currentTimeMillis()) );
                transferRepository.save(transfer);
            }

        }
        Personnel save = personnelService.save(personnel);
        if (save!=null){
            return Result.success(save);
        }else {
            return Result.fail();
        }
    }
    @DeleteMapping
    public Result deleteById(@RequestParam("id") int id){
        int i = personnelService.deleteById(id);
        if (i>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

}
